package loppy.collision 
{
  import loppy.dynamics.Proxy;
  import loppy.dynamics.Shape;
  import loppy.loppy_internal;
  import loppy.utils.InList;
  import loppy.utils.ObjectPool;
  
  public class NSquared implements IBroadphase
  {
    use namespace loppy_internal;
    
    private var proxies :Vector.<Proxy> = new Vector.<Proxy>();
    
    function NSquared()
    { }
    
    public function add(proxy:Proxy):void
    {
      proxies.push(proxy);
    }
    
    public function remove(proxy:Proxy):void
    {
      proxies.splice(proxies.indexOf(proxy), 1);
    }
    
    public function calculatePairs(pairs:InList):void
    {
      //add every pair
      for (var i:int = 0, lenI:int = proxies.length - 1; i < lenI; ++i)
      {
        for (var j:int = i + 1, lenJ:int = proxies.length; j < lenJ; ++j)
        {
          var proxy1:Proxy = proxies[i];
          var proxy2:Proxy = proxies[j];
          
          //shapes belong to different bodies
          if (proxy1.parent.parent != proxy2.parent.parent)
          {
            //check proxy collision first
            if (Collision.collideProxies(proxy1, proxy2))
            {
              var pair:ShapePair = ShapePair.pool.get();
              pair.shape1 = proxy1.parent;
              pair.shape2 = proxy2.parent;
              pairs.add(pair);
            }
          }
        }
      }
    }
  }
}
